package com.yjx.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.mapper.CarchargeMapper;
import com.yjx.mapper.ParkingMapper;
import com.yjx.service.CarchargeService;
import com.yjx.service.ParkingService;
import com.yjx.vo.Carcharge;
import com.yjx.vo.Parking;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 车位缴费服务实现类
 */
@Service
public class CarchargeServiceImpl extends ServiceImpl<CarchargeMapper, Carcharge>
    implements CarchargeService{
    @Resource
    private CarchargeMapper carchargeMapper;

    @Resource
    private ParkingMapper parkingMapper;

    /**
     * 多条件分页查询，借助分页工具返回pageinfo
     * @param page
     * @param limit
     * @param carcharge
     * @return
     */
    @Override
    public PageInfo<Carcharge> queryCarchargeAll(Integer page, Integer limit, Carcharge carcharge) {
        PageHelper.startPage(page,limit);
        List<Carcharge> list = carchargeMapper.queryCarchargeAll(carcharge);
        PageInfo<Carcharge> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addCarcharge(Carcharge carcharge) {
        //查询所有使用中的车位
        List<Parking> parkings = parkingMapper.queryParkingByStatus(1);
        int count = parkings.size();
        //遍历车位，设置默认值
        int i = 0;
        for (Parking parking : parkings) {
            //设置车位Id
            carcharge.setParkId(parking.getId());
            //设置户主Id
            carcharge.setOwnerId(parking.getOwnerId());
            //设置缴费状态
            carcharge.setStatus(0);
            //设置缴费类型为停车费
            carcharge.setType("停车费");
          //添加车位信息
        int j = baseMapper.insert(carcharge);
        i+=j;
        }
        System.out.println(i==count);
    }

    @Override
    public void deleteCarcharge(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        baseMapper.deleteBatchIds(list);
    }

}




